package com.itheima.service.impl;

import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

public class UserServiceImpl implements UserService {
    //1. 创建SqlSessionFactory 工厂对象
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();
    @Override
    public void addManager(User user) {
        //2.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.使用mapper调用方法
        mapper.addManager(user);
        //5.提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public void addNormalUser(User user) {
        //2.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.使用mapper调用方法
        mapper.addNormalUser(user);
        //5.提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    @Override
    public User selectManager(User user) {
        //2.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.使用mapper调用方法
        User manager=mapper.selectManager(user);
        //5.提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return manager;
    }




    @Override
    public User selectNormalUser(User user) {
        //2.获取sqlSession对象
        SqlSession sqlSession = factory.openSession();
        //3.获取brandMapper
        UserMapper mapper = sqlSession.getMapper(UserMapper.class);
        //4.使用mapper调用方法
        User normalUser=mapper.selectNormalUser(user);
        //5.提交事务
        sqlSession.commit();
        //释放资源
        sqlSession.close();
        return normalUser;
    }
}
